#include <gtk/gtk.h>

static void activate(GtkApplication* app, gpointer user_data) {
    GtkWidget *window;

    // 创建一个新窗口，并设置为GTK Application的主窗口
    window = gtk_application_window_new(app);
    gtk_window_set_title(GTK_WINDOW(window), "Hello World");
    gtk_window_set_default_size(GTK_WINDOW(window), 400, 300);
    g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);


    // 显示窗口
    gtk_widget_show_all(window);

    // 窗口显示后，获取屏幕的尺寸并计算居中位置
    GdkRectangle monitor_geometry;
    GdkScreen *screen = gtk_window_get_screen(GTK_WINDOW(window));
    GdkMonitor *monitor = gdk_display_get_primary_monitor(gdk_screen_get_display(screen));
    gdk_monitor_get_geometry(monitor, &monitor_geometry);

    // 计算窗口的新位置使其居中
    gint window_width = gtk_widget_get_allocated_width(window);
    gint window_height = gtk_widget_get_allocated_height(window);
    gint x = (monitor_geometry.width - window_width) / 2;
    gint y = (monitor_geometry.height - window_height) / 2;

    // 移动窗口到新位置
    gtk_window_move(GTK_WINDOW(window), x, y);
}

int main(int argc, char **argv) {
    GtkApplication *app;
    int status;

    // 初始化GTK Application
    app = gtk_application_new("org.gtk.example", G_APPLICATION_DEFAULT_FLAGS);
    g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
    status = g_application_run(G_APPLICATION(app), argc, argv);
    g_object_unref(app);

    return status;
}